from .baseGraphNode import  *
from ..execute.baseExeHis import BaseExeHis


class ToolNode(BaseGraphNode):
    def __init__(self, id, data, graphNodeManager, random_key):
        super().__init__(id, data, graphNodeManager, random_key)
        self.tool_name = data['data']['toolname']
        self.tool_description = data['data']['description']
        self.custom_keys = []
        if 'custom_keys' in data['data']:
            self.custom_keys = data['data']['custom_keys'] 
            '''
                {
                id: "custom-key-name-msg"
                name : "name"
                note: "the name of the tool"
                type:'str'}
            '''
        
        self.executed_num = 1
        for custom_key in self.custom_keys:
            self.dict['out'][custom_key['name']] = {'type':custom_key['type'], 'note':custom_key['note']}
            self.dict['out'][custom_key['id']] = {}

    def get_newest_output_in_session(self, session_info, target_handle):
        '''
        ## Tool Functions:\nThe following tool functions are available in the format of\n```\n{index}. {function name}: {function description}\n{argument1 name} ({argument type}): {argument description}\n{argument2 name} ({argument type}): {argument description}\n...\n```\n\n1. read_file: Read the file and return the content.\n\troot_path (string): The root path of now student's workspace.\n\tfilepath (string): The path to the file.\n2. write_file: Write content to the file.\n\tcontent (string): The content to be written to the file.\n\tfilepath (string): The path to the file.\n\troot_path (string): The root path of now student's workspace.\n
        '''
        tool_description = f'{self.tool_name}: {self.tool_description}\n'
        for custom_key in self.custom_keys:
            name = custom_key['name']
            tp = custom_key['type']
            note = custom_key['note']
            tool_description += f'\t{name} ({tp}): {note}\n'
        return tool_description
    
    def get_tool_name(self):
        return self.tool_name